package com.probox.modules.controller.customer;

import com.probox.common.core.domain.R;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.core.utils.poi.ExcelUtil;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.entity.service.customer.ICustomerInfoAccountService;
import com.probox.common.entity.vo.customer.CustomerInfoLevelVo;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import com.probox.common.security.annotation.PreAuthorize;
import com.probox.customer.api.domain.CustomerInfoAccount;
import com.probox.systemApi.model.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * 客户账号Controller
 *
 * @author zhusiyu
 * @date 2021-05-25
 */
@Api(value = "客户模块", tags = {"客户账号信息后台操作接口"})
@RestController
@RequestMapping("/customer/customerAccount")
public class CustomerInfoAccountController extends BaseController {
    @Autowired
    private ICustomerInfoAccountService customerInfoAccountService;

    /**
     * 查询客户账号列表
     */
    @ApiOperation(value="查询客户账号列表")
    @PreAuthorize(hasPermi = "customerAccount:customerAccount:list")
    @GetMapping("/list")
    public TableDataInfo list(CustomerInfoAccount customerInfoAccount) {
        startPage();
        List<CustomerInfoAccount> list = customerInfoAccountService.queryList(customerInfoAccount);
        return getDataTable(list);
    }

    /**
     * 导出客户账号列表
     */
    @ApiOperation(value="导出客户账号列表")
    @PreAuthorize(hasPermi = "customerAccount:customerAccount:export")
    @Log(title = "客户账号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerInfoAccount customerInfoAccount) throws IOException {
        List<CustomerInfoAccount> list = customerInfoAccountService.queryList(customerInfoAccount);
        ExcelUtil<CustomerInfoAccount> util = new ExcelUtil<CustomerInfoAccount>(CustomerInfoAccount.class);
        util.exportExcel(response, list, "客户账号数据");
    }

    /**
     * 获取客户账号详细信息
     */
    @ApiOperation(value="获取客户账号详细信息")
    @PreAuthorize(hasPermi = "customerAccount:customerAccount:query")
    @GetMapping(value = "/{customerAccountUid}")
    public AjaxResult getInfo(@PathVariable("customerAccountUid") Long customerAccountUid) {
        return AjaxResult.success(customerInfoAccountService.selectCustomerInfoAccountById(customerAccountUid));
    }

    /**
     * 新增客户账号
     */
    @ApiOperation(value="新增客户账号")
    @PreAuthorize(hasPermi = "customerAccount:customerAccount:add")
    @Log(title = "客户账号", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerInfoAccount customerInfoAccount) {
        return toAjax(customerInfoAccountService.insertCustomerInfoAccount(customerInfoAccount));
    }

    /**
     * 修改客户账号
     */
    @ApiOperation(value="修改客户账号")
    @PreAuthorize(hasPermi = "customerAccount:customerAccount:edit")
    @Log(title = "客户账号", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerInfoAccount customerInfoAccount) {
        return toAjax(customerInfoAccountService.updateCustomerInfoAccount(customerInfoAccount));
    }

    /**
     * 删除客户账号
     */
    @ApiOperation(value="删除客户账号")
    @PreAuthorize(hasPermi = "customerAccount:customerAccount:remove")
    @Log(title = "客户账号", businessType = BusinessType.DELETE)
    @DeleteMapping("/{customerAccountUids}")
    public AjaxResult remove(@PathVariable Long[] customerAccountUids) {
        return toAjax(customerInfoAccountService.deleteCustomerInfoAccountByIds(customerAccountUids));
    }

    /**
     * 获取当前用户信息
     */
    @ApiOperation(value="获取当前用户信息")
    @GetMapping("/info/{username}")
    public R<LoginUser> info(@PathVariable("username") String username) {
        CustomerInfoAccount customerInfoAccount = customerInfoAccountService.selectUserByUserName(username);
        if (StringUtils.isNull(customerInfoAccount)) {
            return R.fail("用户名或密码错误");
        }
        LoginUser sysUserVo = new LoginUser();
        sysUserVo.setCustomerInfoAccount(customerInfoAccount);
        return R.ok(sysUserVo);
    }

    /**
     * 锁定或启动账号信息
     */
    @ApiOperation(value="锁定或启动账号信息")
    @PostMapping("/updateAccountStatus")
    public AjaxResult updateAccountStatus(@RequestBody CustomerInfoLevelVo customerInfo) {
        return AjaxResult.success(customerInfoAccountService.updateCustomerAccountStatus(customerInfo));
    }

    /**
     * 锁定或启动指定账号信息
     */
    @ApiOperation(value="锁定或启动指定账号信息")
    @PostMapping("/updateAccountStatusById")
    public AjaxResult updateAccountStatusById(@RequestBody CustomerInfoAccount customerInfoAccount) {
        return AjaxResult.success(customerInfoAccountService.updateAccountStatusById(customerInfoAccount));
    }

}
